<?php
class MiscUtils {
  public static function dump($obj, $what = NULL) {
    if (defined ('DEBUG_ENABLED') && ! DEBUG_ENABLED) {
      return;
    }
    echo "<pre>\n";
    if (is_null ($obj)) {
      echo ($what !== NULL) ? "$what=!NULL!" : '!NULL!';
    } else if ($obj === '') {
      echo ($what !== NULL) ? "$what=!EMPTY!" : '!EMPTY!';
    } else if ($obj === false) {
      echo ($what !== NULL) ? "$what=!FALSE!" : '!FALSE!';
    } else {
      if ($what !== NULL) {
        echo "$what=<br>";
      }
      echo htmlspecialchars (print_r ($obj, true));
    }
    echo "</pre>\n";
  }

  public static function getParam($itm, $default = '', $trimText = false) {
    if (isset($_POST[$itm])) {
      return ($trimText) ? trim($_POST[$itm]) : $_POST[$itm];
    } else if (isset($_GET[$itm])) {
      return ($trimText) ? trim($_GET[$itm]) : $_GET[$itm];
    } else {
      return $default;
    }
  }

  public static function digits($value, $size) {
    $prefix = '';
    for ($i = 0, $il = $size - strlen($value); $i < $il; $i++) {
      $prefix .= '0';
    }
    return $prefix . $value;
  }

  public static function unique() {
    $unique = 'u' . number_format((mktime() * rand()), 0, '.', '');
    return substr($unique, 0, 16);
  }

  public static function createMenuInst($arr, $node) {
    if (!isset($node) || $node == NULL) {
      $node = new stdClass();
      $node->id = 0;
      $node->name = 'ROOT';
      $node->c = array();
      MiscUtils::createMenuInst($arr, $node);
      return $node;
    }

    for ($i = 0, $il = count($arr); $i < $il; $i++) {
      $tmp = $arr[$i];
      if ($tmp->parent_id == $node->id) {
        $n = $tmp;
        $n->c = array();
        $node->c[] = $n;
        MiscUtils::createMenuInst($arr, $n);
      }
    }
  }

  public static function rrmdir($dir) {
    if (is_dir($dir)) {
      $objects = scandir($dir);
      foreach ($objects as $object) {
        if ($object != "." && $object != "..") {
          if (filetype($dir . "/". $object) == "dir") {
            MiscUtils::rrmdir($dir . "/". $object);
          } else {
            unlink($dir . "/" . $object);
          }
        }
      }
      reset($objects);
      rmdir($dir);
    }
  }
}
?>